package com.sjzd.menu;

import com.sjzd.ConfigParser;
import com.sjzd.entity.Bullet;
import com.sjzd.entity.Tank;
import com.sjzd.exception.NullObjectException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class MainMenu extends JFrame {
    /**
     *  消除双缓冲的背景图片
     */
    private Image offBackgroundImage = null;
    // 配置解析器
    private static ConfigParser configParser = null;
    /**
     *  游戏界面大小
     */
    private static Integer WINDOW_WIDTH = 0;
    private static Integer WINDOW_HEIGHT = 0;

    static {
        configParser = ConfigParser.getInstance();
        WINDOW_WIDTH = configParser.getWINDOW_WIDTH();
        WINDOW_HEIGHT = configParser.getWINDOW_HEIGHT();
    }

    private GameMenu gameMenu = null;

    public MainMenu(GameMenu gameMenu) {
        this.gameMenu = gameMenu;
        setTitle("坦克大战网络版");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setVisible(true);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        addMouseListener(new MouseMonitor());
        addKeyListener(new KeyMonitor());
    }


    // 消除双缓冲
    @Override
    public void update(Graphics g) {
        if(offBackgroundImage == null) {
            offBackgroundImage = this.createImage(WINDOW_WIDTH, WINDOW_HEIGHT);
        }
        Graphics gOffScreen = offBackgroundImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.GREEN);
        gOffScreen.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offBackgroundImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawTank(g);
        drawBullet(g);
    }

    private void drawBullet(Graphics g) {
        for ( Bullet bullet : gameMenu.getBulletList()) {
            bullet.draw(null, configParser, g);
        }
    }

    private void drawTank(Graphics g) {
        Color original = g.getColor();
        gameMenu.getTank().draw(null, configParser, g);
        for (Tank other : gameMenu.getOtherTankList()) {
            other.draw(null, configParser, g);
        }
        g.setColor(original);
    }

    // 鼠标控制器
    private class MouseMonitor extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            int targetX = e.getXOnScreen();
            int targetY = e.getYOnScreen();
            gameMenu.getTank().changeTarget(targetX, targetY);
            gameMenu.getTank().getGun().changeTarget(targetX, targetY);
        }
    }
    // 键盘控制器
    private class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_SPACE :
                    if (gameMenu.getTank().isExist) {
                        try {
                            gameMenu.getTank().fire();
                        } catch (NullObjectException ex) {
                            ex.printStackTrace();
                        }
                    }
                    break;
            }
        }
    }
}
